%for vertical grid line optimizing
%vspotcoff1:coordinate value of grid line in vertical
%flag: flag for setting parameter,0-set,1-no
%flagend: flag for optimizing times
%errorlimit: flag for terminal error
%vspotcoff2: cooridnate value after optimizing
%vdmean:mean distance 
%vdermean:mean distance error
function [vspotcoff2,vdmean,vdermean]=vgridadjust(vspotcoff1,flag,flagend,errorlimit)
[s,vf1]=size(vspotcoff1);
disp(['number of vertial grid lines��',num2str(vf1)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%distance between two grid lines
for n=1:vf1-1
 vd(n)=vspotcoff1(n+1)-vspotcoff1(n);
end
%figure,plot(1:vf1-1,vd,'mean distance in vertial');
%mean distance
vdmean=mean(vd);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%distance error
for n=1:vf1-1
 vderror(n)=abs(vd(n)-vdmean);
end
%figure,plot(1:vf1-1,vderror,'distance error profile in vertial');
%mean distance error
vdermean=mean(vderror);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%mean square error
verror=0;
for m=1:vf1-1
    verror=verror+(vd(m)-vdmean).^2;
end
verror=sqrt(verror/(vf1-1))
disp(['mean distance in vertical��',num2str(vdmean),'mean distance error in vertical��',num2str(vdermean)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%estimate the number of wrong grid lines
vernum=0;
for n=2:vf1-2
    if vderror(n)>verror+vdmean/10
        vernum=vernum+1;
        vergridline(vernum)=n;
    end
end
disp(['the wrong grid line numbers in vertical:',num2str(vernum)]);
if flag==0
    %initialization of flagend and errorlimit
    flagend=vernum;
    errorlimit=2*vdermean;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%find the maximum error
if flagend~=0 && vdermean<errorlimit
if vernum~=0 %&& flagend~=0 
       [maxerror,index]=max(vderror);
       disp(['the maximum error is��',num2str(index)]);
       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       %the first grid line
       if index==1
        if vd(index)<vdmean && (vderror(index+1)<verror+vdmean/10)
            disp(['redundant grid line 1:',int2str(index)]);
            vspotcoff2(index)=(vspotcoff1(index)+vspotcoff1(index+1))/2;
           for k=index+1:vf1-1
               vspotcoff2(k)=vspotcoff1(k+1);
           end
         elseif vd(index)<vdmean && (vderror(index+1)>verror+vdmean/10)
           disp(['redundant grid line 2:',int2str(index)]);
           vspotcoff2(index)=vspotcoff1(index);
           vspotcoff2(index+1)=(vspotcoff1(index+1)+vspotcoff1(index+2))/2;
           for k=index+2:vf1-1
               vspotcoff2(k)=vspotcoff1(k+1);
           end
         elseif vd(index)>vdmean && (vderror(index+1)<verror+vdmean/10)
           disp(['missing a grid line 1:',int2str(index)]);
           vspotcoff2(index)=vspotcoff1(index);
           vspotcoff2(index+1)=(vspotcoff1(index)+vspotcoff1(index+1))/2;
           vspotcoff2(index+2:vf1+1)=vspotcoff1(index+1:vf1);
           
         elseif vd(index)>vdmean &&  (vderror(index+1)>verror+vdmean/10)
           disp(['missing a grid line 2:',int2str(index)]);
           vspotcoff2(index)=vspotcoff1(index);
           vspotcoff2(index+1)=vspotcoff1(index)+(vspotcoff1(index+2)-vspotcoff1(index))/3;
           vspotcoff2(index+3:vf1+1)=vspotcoff1(index+2:vf1);
           vspotcoff2(index+2)=vspotcoff1(index)+2*(vspotcoff1(index+2)-vspotcoff1(index))/3;
        else (vd(index)>vdmean && vd(index+1))<vdmean ||(vd(index)<vdmean && vd(index+1)>vdmean)
           disp(['locate at wrong place:',int2str(index)]);
           vspotcoff2=vspotcoff1;
           vspotcoff2(index+1)=(vspotcoff1(index)+vspotcoff1(index+2))/2;
        end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %the last grid line
     elseif index==vf1-1
        if vd(index)<vdmean && (vderror(index-1)<verror+vdmean/10)
            disp(['redundant grid line 1:',int2str(index)]);
            vspotcoff2(1:index-1)=vspotcoff1(1:index-1);
           vspotcoff2(index)=(vspotcoff1(index)+vspotcoff1(index+1))/2;
           
       elseif vd(index)<vdmean && (vderror(index-1)>verror+vdmean/10)
           disp(['redundant grid line 2:',int2str(index)]);
           vspotcoff2(1:index-2)=vspotcoff1(1:index-2);
           vspotcoff2(index-1)=(vspotcoff1(index)+vspotcoff1(index-1))/2;
           vspotcoff2(index)=vspotcoff1(index+1);
           
       elseif vd(index)>vdmean && (vderror(index-1)<verror+vdmean/10)
           disp(['missing a grid line 1:',int2str(index)]);
           vspotcoff2(1:index)=vspotcoff1(1:index);
           vspotcoff2(index+1)=(vspotcoff1(index)+vspotcoff1(index+1))/2;
           vspotcoff2(index+2)=vspotcoff1(index+1);
           
       elseif vd(index)>vdmean &&  (vderror(index-1)>verror+vdmean/10)
           disp(['missing a grid line 2:',int2str(index)]);
           vspotcoff2(1:index-1)=vspotcoff1(1:index-1);
           vspotcoff2(index)=vspotcoff1(index-1)+(vspotcoff1(index+1)-vspotcoff1(index-1))/3;
           vspotcoff2(index+2)=vspotcoff1(index+1);
           vspotcoff2(index+1)=vspotcoff1(index-1)+2*(vspotcoff1(index+1)-vspotcoff1(index-1))/3;
       else (hd(index)>hdmean && hd(index-1))<hdmean ||(hd(index)<vdmean && vd(index-1)>vdmean)
           disp(['locate at wrong place:',int2str(index)]);
           vspotcoff2=vspotcoff1;
           vspotcoff2(index)=(vspotcoff1(index+1)+vspotcoff1(index-1))/2;
        end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    else  
        if (vd(index)<vdmean && (vderror(index-1)<verror+vdmean/10)&& (vderror(index+1)<verror+vdmean/10))||(vd(index)<vdmean &&vd(index+1)<vdmean)
            disp(['redundant grid line 1:',int2str(index)]);
            vspotcoff2(1:index-1)=vspotcoff1(1:index-1);
           vspotcoff2(index)=(vspotcoff1(index)+vspotcoff1(index+1))/2;
           for k=index+1:vf1-1
               vspotcoff2(k)=vspotcoff1(k+1);
           end
       elseif vd(index)<vdmean && ( (vderror(index+1)>verror+vdmean/10)||( vderror(index-1)>verror+vdmean/10))
           disp(['redundant grid line 2:',int2str(index)]);
           vspotcoff2(1:index)=vspotcoff1(1:index);
           vspotcoff2(index+1)=(vspotcoff1(index+1)+vspotcoff1(index+2))/2;
           for k=index+2:vf1-1
               vspotcoff2(k)=vspotcoff1(k+1);
           end
       elseif vd(index)>vdmean && (vderror(index-1)<verror+vdmean/10)&& (vderror(index+1)<verror+vdmean/10)
           disp(['missing a grid line 1:',int2str(index)]);
           vspotcoff2(1:index)=vspotcoff1(1:index);
           vspotcoff2(index+1)=(vspotcoff1(index)+vspotcoff1(index+1))/2;
           vspotcoff2(index+2:vf1+1)=vspotcoff1(index+1:vf1);
           
       else vd(index)>vdmean && ( (vderror(index-1)>verror+vdmean/10)|| (vderror(index+1)>verror+vdmean/10))
           disp(['missing a grid line 2:',int2str(index)]);
           if vd(index-1)>vdmean
              vspotcoff2(1:index-1)=vspotcoff1(1:index-1);
              vspotcoff2(index)=vspotcoff1(index-1)+(vspotcoff1(index+1)-vspotcoff1(index-1))/3;
              vspotcoff2(index+1)=vspotcoff1(index-1)+2*(vspotcoff1(index+1)-vspotcoff1(index-1))/3;
              vspotcoff2(index+2:vf1+1)=vspotcoff1(index+1:vf1);
           else
              vspotcoff2(1:index)=vspotcoff1(1:index);
              vspotcoff2(index+1)=vspotcoff1(index)+(vspotcoff1(index+2)-vspotcoff1(index))/3;
              vspotcoff2(index+2)=vspotcoff1(index)+2*(vspotcoff1(index+2)-vspotcoff1(index))/3;
              vspotcoff2(index+3:vf1+1)=vspotcoff1(index+2:vf1);
          end
        end
    
    end
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    flag=flag+1;
    flagend=flagend-1;
    vgridadjust(vspotcoff2,flag,flagend,errorlimit);
 else
    vspotcoff2=vspotcoff1;
    clear vspotcoff1;
    [s,size1]=size(vspotcoff2);
    disp(['final vertical grid line number��',num2str(size1)]);
    disp('no error');
 end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
else
    vspotcoff2=vspotcoff1;
end
clear vspotcoff1;
clear flag;
clear flagend;
clear errorlimit;